package com.aSemy.qotwReader.core.question;

import java.io.*;
import java.net.*;

import com.aSemy.qotwReader.core.types.*;


/**
 * This contains information specific to a single question.
 * When the Question is created it is automatically downloaded and converted.
 * @author Adam Semenenko
 */
public class Question
{
    /** The HTML of the question as downloaded by Jericho. */
    final private QuestionHTML html;
    /** The file where the HTML is saved. */
    final private QuestionFile file;
    /** The file where the question is saved as an ebook. */ 
    final private QuestionEbook epub;
    /** Information such as the title, date, and comments specific to the ebook. */
    final private QuestionData data;
    
    /**
     * Creates a new Question and downloads the HTML, gathers the question data, saves it to file, and converts it to the specified ebook type.
     * @param url The URL of the question.
     * @param reader The type of ebook reader to tailor the book for.
     * @param outputEbook The type of ebook to output.
     * @param directoryOfCalibreEbookConverter The directory which contains the Calibre command line tool ebook converter.
     * @throws IOException Thrown if there is an error downloading the HTML source, or when writing the HTML to file.
     */
    public Question(URL url, ReaderType reader, EbookType outputEbook, File directoryOfCalibreEbookConverter) throws IOException
    {
        System.out.println("Getting question html");
        this.html = new QuestionHTML(url);
        System.out.println("OK");
        
        System.out.println("Getting question data");
        data = new QuestionData(html, reader);
        System.out.println("OK");
        
        System.out.println("Writing question html to local file");
        this.file = new QuestionFile(html, data);
        System.out.println("OK");
        
        System.out.println("Converting to ebook, type " + outputEbook.toString());
        this.epub = new QuestionEbook(file, data);
        
        epub.convertToFormat(outputEbook, directoryOfCalibreEbookConverter);
        
        System.out.println("OK");
    }
}
